package com.huawei.crm.web.controller;

import com.huawei.crm.aspect.PermissionAnnotation;
import com.huawei.crm.domain.Employee;
import com.huawei.crm.event.MyTestEventPubListener;
import com.huawei.crm.service.TestRetryService;
import com.huawei.crm.sync.AsyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author 罗华
 * @date 2021/12/8
 */
@RestController
@RequestMapping("/test")
@Slf4j
public class ClientTestController extends BaseController {


    @Autowired
    private AsyncTask asyncTask;

    @Autowired
    private TestRetryService retryService;

    @Autowired
    private MyTestEventPubListener publisher;

    @PostMapping("/hello")
    private String testHello(@RequestBody @Valid Employee employee) {

        return "hello" + employee.toString();
    }

    @GetMapping(value = "/publishEvent1")
    @PermissionAnnotation
    public String testPublishEvent(String val) {

        publisher.pushListener(val + "我来了！");
        publisher.pushListener2("我又来了");

        return "hello event";
    }

    @GetMapping("/retry")
    @Retryable(backoff = @Backoff(value = 2000))
    public String testRetry() throws Exception {

        log.info("开始调用重试方法begin...");
        retryService.test(0);
        log.info("重试方法调用结束end...");

        return "hello retry";
    }


    @RequestMapping("/task")
    public void task() throws InterruptedException {
        long t1 = System.currentTimeMillis();
        asyncTask.doTask1();
        asyncTask.doTask2();
        Thread.sleep(1000);
        long t2 = System.currentTimeMillis();
        log.info("main cost {} ms", t2 - t1);
    }

    @GetMapping("/provider01")
    public String provider01() {
        return "hi,我是provider01返回的内容(Get)";
    }

    @PostMapping("/provider02")
    public String getById() {
        return "hi,我是provider02返回的内容(Post)";
    }

}
